package cn.yearndr.zfsy.zfsy.service;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.util.List;

@Service
public class HttpRequestService {
    public static String client(String url){
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class);
        return entity.getBody();
    }

//    public static String getBody(String url,String charset){
//        RestTemplate restTemplate = new RestTemplate();
//        List<HttpMessageConverter<?>> list = restTemplate.getMessageConverters();
//        for (HttpMessageConverter<?> httpMessageConverter : list) {
//            if(httpMessageConverter instanceof StringHttpMessageConverter) {
//                ((StringHttpMessageConverter) httpMessageConverter).setDefaultCharset(Charset.forName(charset));
//                break;
//            }
//        }
//        return restTemplate.getForEntity(url, String.class).getBody();
//    }

    /** 百度搜索基本url 后面可以接的参数有 pn rn ie 等 */
    public final static String baseUrl = "https://www.baidu.com/s?ie=utf-8";
    /** 连接超时时间 */
    public static int timeout = 30 * 1000;
    /** 连接重试次数 */
    public static int times = 10;
    /** UA */
    public static String UserAgent[] = {
            "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
            "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
            "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; InfoPath.3; rv:11.0) like Gecko",
            "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36 OPR/37.0.2178.32",
            "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 UBrowser/5.6.12150.8 Safari/537.36",
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36 OPR/37.0.2178.32" };

    /** 获取随机UA */
    public static String getRandomUA() {
        return UserAgent[(int) (Math.random() * (UserAgent.length))];
    }

    /** 在这里进行连接 如果失败会继续重试 */
    public static Document getDocument(String url) {
        Document doc = null;
        for (int i = 0; i < times; i++) {
            try {
                doc = Jsoup.connect(url).header("User-Agent", getRandomUA()).timeout(timeout).get();
                if (doc != null)
                    break;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return doc;
    }
}
